package com.haoji.upload.common.config.rocketmq;

import com.haoji.upload.dto.MqConsumerBeanEnum;
import com.haoji.upload.rocketmq.MqConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * @program: upload
 * @description:
 * @author: xiaolei
 * @create: 2021-04-21 10:06
 **/
@Slf4j
@Configuration
public class DefaultConsumerListener extends AbstractConsumerListener implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {
    /**
     * 上下文
     */
    private ApplicationContext applicationContext;

    @Autowired
    private MqProperties mqProperties;

    @Override
    public ConsumeConcurrentlyStatus onMessage(List<MessageExt> msgs) {
        for (MessageExt msg : msgs) {
            String topicTag = msg.getTopic() + ":" + msg.getTags();
            MqConsumerBeanEnum mqConsumerBeanEnum = MqConsumerBeanEnum.getBeanByTopicTag(topicTag);
            if (mqConsumerBeanEnum == null) {
                break;
            }
            ConsumeConcurrentlyStatus consumerStatus = null;
            try {
                Object serviceBean = applicationContext.getBean(mqConsumerBeanEnum.getBeanName());
                if (serviceBean == null) {
                    break;
                }
                if (serviceBean instanceof MqConsumerService) {
                    String message = new String(msg.getBody(), "utf-8");
                    MqConsumerService consumerService = (MqConsumerService) serviceBean;
                    // 预处理
                    consumerService.beforeHandler(message);
                    // 处理
                    consumerStatus = consumerService.handle(message);
                    // 处理之后
                    consumerService.afterHandler(message, new Date(), consumerStatus);
                }
            } catch (UnsupportedEncodingException e) {
                log.error("body转字符串解析失败");
            }
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
            // 订阅同一个topic下的多个tag
            super.listener("T_DEMO_PROJECT", "Article || ADD_ORDER");
        } catch (MQClientException e) {
            log.error("consumer error");
        }
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
